package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理项目Service实现类
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.selectByPage(name, status);
        PageResponse<NursingProjectVo> pageResponse = new PageResponse(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 新增护理项目逻辑
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);

        //调用mapper层
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id查询护理项目逻辑
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        // 调用mapper层
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        // 响应结果
        return BeanUtil.toBean(nursingProject,NursingProjectVo.class);
    }

    /**
     * 更新护理项目信息逻辑
     * @param nursingProjectDto
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        // 调用mapper层
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        // 响应结果
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 修改护理项目状态逻辑
     * @param id
     * @param status
     */
    @Override
    public void getByIdStatus(Long id, int status) {
        // 调用mapper层
        nursingProjectMapper.updateStatus(id,status);
    }

    /**
     * 删除护理项目逻辑
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 调用mapper层
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目逻辑
     * @return
     */
    @Override
    public List<NursingProjectVo> getAll() {
        // 调用mapper层
        List<NursingProject> nursingProjects = nursingProjectMapper.getAll();
        // 响应结果
        return nursingProjects.stream().map(nursingProject -> {
            // 创建护理项目视图对象
            NursingProjectVo nursingProjectVo = new NursingProjectVo();
            // 将护理项目实体拷贝到护理项目视图对象
            BeanUtils.copyProperties(nursingProject,nursingProjectVo);
            // 返回护理项目视图对象
            return nursingProjectVo;
        }).collect(Collectors.toList());
    }

}